//
//  WeatherCondition.swift
//  MyDiary2025
//
//  Created by 罗万能 on 2025/6/26.
//
import SwiftUI

enum WeatherCondition: String, CaseIterable, Identifiable ,Codable{
    // 随机生成天气的静态函数
        static func random() -> WeatherCondition {
            // 所有天气情况的数组
            let allConditions: [WeatherCondition] = [
                .sunny, .cloudy, .mostlySunny, .mostlyCloudy,
                .rainy, .thunderstorm, .snowy, .foggy,
                .windy, .partlyCloudyDay, .partlyCloudyNight,
                .haze, .drizzle, .sleet, .blizzard
            ]
            
            // 根据当前时间设置随机种子，确保每次结果不同
            srand48(Int(Date().timeIntervalSince1970))
            
            // 随机选择一种天气（可以根据需要调整概率分布）
            return allConditions.randomElement() ?? .sunny
        }
    
    case sunny              // 晴天
    case cloudy             // 多云
    case mostlySunny        // 晴转多云
    case mostlyCloudy       // 多云转晴
    case rainy              // 雨天
    case thunderstorm       // 雷暴
    case snowy              // 雪天
    case foggy              // 雾天
    case windy              // 大风
    case partlyCloudyDay    // 少云（白天）
    case partlyCloudyNight  // 少云（夜晚）
    case haze               // 雾霾
    case drizzle            // 小雨
    case sleet              // 雨夹雪
    case blizzard           // 暴风雪
    
    // 用于唯一标识每个枚举值
    var id: String { rawValue }
    
    // 返回对应的 SF Symbol 名称
    var symbolName: String {
        switch self {
        case .sunny: return "sun.max.fill"
        case .cloudy: return "cloud.fill"
        case .mostlySunny: return "cloud.sun.fill"
        case .mostlyCloudy: return "cloud.sun.fill"
        case .rainy: return "cloud.rain.fill"
        case .thunderstorm: return "cloud.bolt.rain.fill"
        case .snowy: return "cloud.snow.fill"
        case .foggy: return "cloud.fog.fill"
        case .windy: return "wind"
        case .partlyCloudyDay: return "cloud.sun.fill"
        case .partlyCloudyNight: return "cloud.moon.fill"
        case .haze: return "sun.haze.fill"
        case .drizzle: return "cloud.drizzle.fill"
        case .sleet: return "cloud.sleet.fill"
        case .blizzard: return "wind.snow"
        }
    }
    
    // 返回中文描述
    var chineseDescription: String {
        switch self {
        case .sunny: return "晴天"
        case .cloudy: return "多云"
        case .mostlySunny: return "晴转多云"
        case .mostlyCloudy: return "多云转晴"
        case .rainy: return "雨天"
        case .thunderstorm: return "雷暴"
        case .snowy: return "雪天"
        case .foggy: return "雾天"
        case .windy: return "大风"
        case .partlyCloudyDay: return "少云（白天）"
        case .partlyCloudyNight: return "少云（夜晚）"
        case .haze: return "雾霾"
        case .drizzle: return "小雨"
        case .sleet: return "雨夹雪"
        case .blizzard: return "暴风雪"
        }
    }
    
    // 返回适合显示的颜色
    var color: Color {
        switch self {
        case .sunny, .mostlySunny, .partlyCloudyDay:
            return .yellow.opacity(0.8)
        case .cloudy, .mostlyCloudy, .foggy, .haze:
            return .gray
        case .rainy, .drizzle, .sleet:
            return .blue.opacity(0.7)
        case .thunderstorm:
            return .purple.opacity(0.8)
        case .snowy, .blizzard:
            return .white.opacity(0.9)
        case .windy:
            return .cyan.opacity(0.7)
        case .partlyCloudyNight:
            return .indigo.opacity(0.8)
        }
    }
    
    // 返回可直接显示的 Image 视图
    var image: Image {
       return Image(systemName: symbolName)
//            .foregroundColor(color) as! Image
    }
    
    // 返回推荐建议
    var recommendation: String {
        switch self {
        case .sunny: return "记得涂防晒霜，戴太阳镜"
        case .rainy, .drizzle, .sleet: return "出门记得带伞"
        case .snowy, .blizzard: return "注意保暖，道路可能结冰"
        case .windy: return "小心高空坠物，避免在广告牌下行走"
        case .foggy, .haze: return "建议佩戴口罩，注意交通安全"
        default: return "天气良好，适合正常出行"
        }
    }
}

extension WeatherCondition {
//    func image(size: CGFloat = 20, weight: Font.Weight = .regular) -> Image {
//        Image(systemName: symbolName)
//            .font(.system(size: size, weight: weight))
//            .foregroundColor(color) as! Image
//    }
}
